﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MyWpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MyViewModel();
            Task.Run(() => InitializeDatabaseAsync());
        }

        private async void OnLoaded(object sender, RoutedEventArgs e)
        {
            await InitializeDatabaseAsync();
        }

        public async Task InitializeDatabaseAsync()
        {
            try
            {
                using (var db = new ProjectDataBase.ProjectDbContext())
                {
                    // 执行一次数据库操作以确保表和连接初始化完成
                    await db.TestDatas.FirstOrDefaultAsync();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("初始化数据库异常" + ex);
            }

        }
    }
}
